package com.zzsy.shoppingpro.dialog;

import android.app.Activity;
import android.app.Dialog;
import android.content.res.Resources;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import com.zzsy.shoppingpro.R;

/**
 * Created by Administrator on 2019/8/30 0030.
 */

public class ComTwoBtnDialog extends Dialog implements View.OnClickListener {

    private Activity activity;
    private TextView tvTitle;
    private TextView leftBtn;
    private TextView rightBtn;

    private String leftBtnStr;
    private int leftTextColor;
    private String rightBtnStr;
    private int rightTextColor;

    private String titleStr;

    public static final int DIALOG_LOGIN = 1;

    public ComTwoBtnDialog(@NonNull Activity context, int dialogType) {
        this(context, R.style.BottomPopupDialog, dialogType);
    }

    public ComTwoBtnDialog(@NonNull Activity context, int themeResId, int dialogType) {
        super(context, themeResId);
        this.activity = context;

        Resources res = activity.getResources();
        switch (dialogType) {
            case DIALOG_LOGIN:
                titleStr = "您还没有登录哦！";
                leftBtnStr = "取消";
                leftTextColor = res.getColor(R.color.not_psd);

                rightBtnStr = "确定";
                rightTextColor = res.getColor(R.color.xu_hua_xian);
                break;
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setCanceledOnTouchOutside(false);
        setCancelable(false);
        setContentView(R.layout.dialog_com_two);

        initUI();
    }

    /**
     * 初始化数据
     */
    private void initUI() {
        //提示文字
        tvTitle = findViewById(R.id.tip_tv);
        leftBtn = findViewById(R.id.left_btn);
        rightBtn = findViewById(R.id.right_btn);
        leftBtn.setOnClickListener(this);
        rightBtn.setOnClickListener(this);

        tvTitle.setText(titleStr);

        setLeftBtnStyles(true,leftBtnStr,leftTextColor);
        setRightBtnStyles(true,rightBtnStr,rightTextColor);
    }

    private void setRightBtnStyles(boolean needShow, String rightBtnStr, int rightTextColor) {
        if (rightBtn != null) {
            rightBtn.setVisibility(needShow ? View.VISIBLE : View.GONE);
            rightBtn.setText(rightBtnStr);
            rightBtn.setTextColor(rightTextColor);
        }
    }

    private void setLeftBtnStyles(boolean needShow, String leftBtnStr, int leftTextColor) {
        if (leftBtn != null) {
            leftBtn.setVisibility(needShow ? View.VISIBLE : View.GONE);
            leftBtn.setText(leftBtnStr);
            leftBtn.setTextColor(leftTextColor);
        }
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.left_btn:
                iDialogBtnClickListener.onDialogLeftBtnClick();
                dismiss();
                break;
            case R.id.right_btn:
                iDialogBtnClickListener.onDialogRightBtnClick();
                dismiss();
                break;
        }



    }

    @Override
    public void show() {
        super.show();
        Window window = getWindow();
        window.setWindowAnimations(R.style.dialogWindowAnim); // 设置窗口弹出动画
        WindowManager.LayoutParams layoutParams = activity.getWindow().getAttributes();
        layoutParams.alpha = 0.5f;
        layoutParams.dimAmount = 0.5f;
        activity.getWindow().setAttributes(layoutParams);
    }

    @Override
    public void dismiss() {
        super.dismiss();
        WindowManager.LayoutParams layoutParams = activity.getWindow().getAttributes();
        layoutParams.alpha = 1.0f;
        layoutParams.dimAmount = 1.0f;
        activity.getWindow().setAttributes(layoutParams);
    }

    public interface  IDialogBtnClickListener{
        void onDialogLeftBtnClick();
        void onDialogRightBtnClick();
    }

    private IDialogBtnClickListener iDialogBtnClickListener;

    public void setIDialogBtnClickListener(IDialogBtnClickListener DialogBtnClickListener){
        this.iDialogBtnClickListener = DialogBtnClickListener;
    }
}
